package com.sengled.cloud.biz.union.mgt.dao.sys;

import java.util.List;

import com.sengled.cloud.biz.union.mgt.model.vo.*;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.sengled.cloud.biz.union.mgt.dao.sys.provider.UserDaoProvider;
import com.sengled.cloud.biz.union.mgt.model.po.User;
import com.sengled.cloud.biz.union.mgt.model.po.UserRole;
import com.sengled.cloud.biz.union.mgt.model.request.UserListQueryRequest;

@Mapper
public interface UserDao {
	@Update("UPDATE user SET seal_mark=1 WHERE id=#{id}")
    int deleteByPrimaryKey(@Param("id") Long id);
   
    @Insert("INSERT INTO user(user_code,user_name,sex,company_id,shop_id,profile_photo,id_number,contact,work_condition,hire_date,leave_date,create_date,operator_id,seal_mark) " +
            "VALUES(#{user.userCode},#{user.userName},#{user.sex},#{user.companyId},#{user.shopId},#{user.profilePhoto},#{user.idNumber},#{user.contact},#{user.workCondition},#{user.hireDate},#{user.leaveDate},#{user.createDate},#{user.operatorId},0)")
    @Options(useGeneratedKeys = true, keyProperty = "user.id")
    int insert(@Param("user") User user);

    @Insert("INSERT INTO user_role(role_id,user_id,create_date,seal_mark) " +
            "VALUES(#{userRole.roleId},#{userRole.userId},#{userRole.createDate},0)")
    @Options(useGeneratedKeys = true, keyProperty = "userRole.id")
    int insertRole(@Param("userRole") UserRole userRole);
    
    int insertSelective(User record);
	
    @Select("SELECT user_code userCode,user_name userName,r.role_name roleName,s.shop_name shopName, CASE WHEN sex=1 THEN '女' ELSE '男' END sex,id_number idNumber,CASE WHEN work_condition=2 THEN '离职' WHEN work_condition=3 THEN '休假' ELSE '在职' END workCondition,contact,DATE_FORMAT(hire_date,'%Y-%m-%d') hireDate,DATE_FORMAT(leave_date,'%Y-%m-%d') leaveDate FROM user u,role r,user_role ur,shop s WHERE u.id=ur.user_id and r.id=ur.role_id and u.shop_id=s.id and u.id=#{id}")
    UserDetailVo selectByPrimaryKey(@Param("id") Long id);

    int updateByPrimaryKeySelective(User record);
    
 
    
    @Update("UPDATE user SET user_code=#{user.userCode},user_name=#{user.userName},sex=#{user.sex},shop_id=#{user.shopId},id_number=#{user.idNumber},contact=#{user.contact},work_condition=#{user.workCondition}"
    		+ ",hire_date=#{user.hireDate},leave_date=#{user.leaveDate},company_id=#{user.companyId},operator_id=#{user.operatorId},modify_date=#{user.modifyDate} WHERE id=#{user.id}")
    int updateByPrimaryKey(@Param("user") User user);
    
    
    @Insert("update user_role set role_id=#{userRole.roleId},modify_date=#{userRole.modifyDate},seal_mark=0 where user_id=#{userRole.userId}")
    int updateRole(@Param("userRole") UserRole userRole);
    
    
    //@Select("SELECT id,user_code userCode,user_name userName,user_tel userTel,CASE WHEN user_nature=1 THEN '直营' WHEN user_nature=1 THEN '加盟' ELSE '其他' END  userNature FROM user WHERE (user_code = #{qp.userCode}) AND (user_name = #{qp.userName}) AND (user_nature=#{qp.userNature}) AND (seal_mark = 0) LIMIT #{qp.start},#{qp.pageSize}")  
    @SelectProvider(type = UserDaoProvider.class, method = "queryUserSQL")
    List<UserVo> listUsersByPage(UserListQueryRequest userListQueryRequest);
    
    @SelectProvider(type = UserDaoProvider.class, method = "queryUserTotalRecords")
    Integer queryuserTotalRecords(UserListQueryRequest userListQueryRequest);


    @Select("select * from user where user_name = #{username}")
    User selectByUsername(String username);

    @Select("select id as id,user_code as userCode,user_name as userName from user where id = #{id}")
    User selectById(@Param("id") Long id);

    @Select("SELECT id,role_name name FROM role where enable_mark=1 group by id,role_name order by id asc ")
    List<SelectBoxDataVo> getRoleSelectData();

    @Select("select sa.id as id,sa.shop_code as code,sa.shop_name as name from user_acct_shop_acct uasa,shop_account sa where sa.seal_mark = 0 and sa.enable_mark = 1 and sa.id = uasa.shop_account_id and uasa.user_account_id = #{id}")
    List<ShopAccountVo> getUserShops(Long id);

    @Update("update user set profile_photo = #{profileName} where id = #{id}")
    int updateUserForProfile(@Param("id") Long id,@Param("profileName") String profileName);

    @Select("select id as id,user_code as code,user_name as name,profile_photo as profilePhoto from user where id = #{id}")
    CurrentUserVo selectCurrentUser(@Param("id") Long id);
    
    
    @Select("SELECT user_code userCode,user_name userName,r.id roleId,r.role_name roleName,s.shop_name shopName,s.id shopId, sex,id_number idNumber,work_condition workCondition,contact,DATE_FORMAT(hire_date,'%Y-%m-%d') hireDate,DATE_FORMAT(leave_date,'%Y-%m-%d') leaveDate FROM user u,role r,user_role ur,shop s WHERE u.id=ur.user_id and r.id=ur.role_id and u.shop_id=s.id and u.id=#{id}")
    UserEditDetailVo getUserEditDetail(@Param("id") Long id);
    
    @Select("select company_id companyId from user_account where id = #{id}")
    Long queryUserCompanyId(@Param("id") Long id);
    
    @Select("SELECT r.id id FROM user u,role r,user_role ur WHERE u.id=ur.user_id AND r.id=ur.role_id  AND r.enable_mark=1 AND r.seal_mark = 0 AND r.role_type=1 and u.id=#{id} limit 1")
    Long getUserRoleIdByUserId(@Param("id") Long id);
    
    
    
    @Delete("delete from user_role WHERE user_id=#{id}")
    int deleteUserRole(@Param("id") Long id);
    
    @Select("SELECT id from user_account where user_id=#{id}")
    Long getUserAccountIdByUserId(@Param("id") Long id);
    
    @Delete("delete from user_account WHERE user_id=#{id}")
    int deleteUserAccount(@Param("id") Long id);
    
   
    @Delete("delete from user_acct_shop_acct WHERE user_account_id in(SELECT id from user_account where user_id=#{id})")
    int deleteUserAcctShopAcct(@Param("id") Long id);
    
    @Delete("delete from user_feedback WHERE user_id=#{id}")
    int deleteUserFeedback(@Param("id") Long id);
    
    @Delete("delete from account_role WHERE user_account_id in(SELECT id from user_account where user_id=#{id})")
    int deleteAccountRole(@Param("id") Long id);
    
    @Select("SELECT count(id) from user where user_code=#{userCode} and seal_mark=0")
    int getUserCountByUserCode(@Param("userCode") String userCode);
}